作法與前幾天介紹的大致相同,只不過需要手動將圖片檔搬移至各語系指定的資料夾下。
以下實作以兩種不同的圖片,分別代表英文和繁體中文
首先設定好起始畫面
新增 English 畫面
新增 繁體中文 畫面
將圖片拉入專案底下
寫個方便呼叫的函數
import UIKit
class Localize: NSObject {
static let shared = Localize()
/// 依語系檔裡的 key 取得 value 值(用於 Label 的 Text)
///
/// - Parameters:
/// - withText: 多國語系檔的 key 值
/// - withLocalizationFileNmae: 多國語系檔的檔名
/// - Returns: String
func localizedText(withText key: String, withLocalizationFileNmae localizationFileNmae: String) -> String {
// 取得 Bundle 下的的多國語系檔
let path = Bundle.main.path(forResource: localizationFileNmae, ofType: "lproj")
let bundle = Bundle(path: path!)
// 依 key 值和 Bundle 的多國語系檔取得對應的 value
return NSLocalizedString(key, tableName: nil, bundle: bundle!, value: "", comment: "")
}
/// 依語系檔裡的 key 取得 value 值(用於 UIImageView)
///
/// - Parameters:
/// - withImageName: 多國語系檔的 key 值
/// - withLocalizationFileNmae: 多國語系檔的檔名
/// - Returns: UIImage
func localizedImage(withImageName key: String, withLocalizationFileNmae localizationFileNmae: String) -> UIImage {
// 取得 Bundle 下的的多國語系檔
let path = Bundle.main.path(forResource: localizationFileNmae, ofType: "lproj")
let bundle = Bundle(path: path!)
// 依 key 值和 Bundle 的多國語系檔取得對應的 value
return UIImage(named: NSLocalizedString(key,bundle: bundle!, comment: ""))!
}
}
在 Localizable.strings 裡寫好對應的多國
English:
繁體中文:
分別在各個語系的畫面裡,將 Label 、 Image View 呼叫其對應的文字與圖片
分別在 EnglishViewController、ChineseViewController 裡
ocalize.shared.localizedText(withText:「填入 Localizable.strings 裡的 Key」, withLocalizationFileNmae: 「填入 多國語系的資料夾檔名」)
``
ocalize.shared.localizedImage(withImageName:「填入 多國語系.lproj 裡的 圖片名稱」, withLocalizationFileNmae: 「填入 多國語系的資料夾檔名」)
``
大功告成!
mport UIKit
lass MainViewController: UIViewController {
@IBOutlet weak var baseLabel: UILabel!
@IBOutlet weak var iconEnglishButton: UIButton!
@IBOutlet weak var iconChineseButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
iconEnglishButton.setTitle("English", for: .normal)
iconChineseButton.setTitle("Chinese", for: .normal)
}
@IBAction func pushToLanguagePage(_ sender: UIButton) {
switch sender{
case iconEnglishButton:
let nextVC = EnglishViewController()
self.navigationController?.pushViewController(nextVC, animated: false)
case iconChineseButton:
let nextVC = ChineseViewController()
self.navigationController?.pushViewController(nextVC, animated: false)
default:
break
}
}
``
mport UIKit
lass EnglishViewController: UIViewController {
@IBOutlet weak var englishLabel: UILabel!
@IBOutlet weak var iconEnglishImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
englishLabel.text = Localize.shared.localizedText(withText: "language", withLocalizationFileNmae: "en")
iconEnglishImageView.image = Localize.shared.localizedImage(withImageName: "English", withLocalizationFileNmae: "en")
}
``
mport UIKit
lass ChineseViewController: UIViewController {
@IBOutlet weak var chineseLabel: UILabel!
@IBOutlet weak var iconChineseImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
chineseLabel.text = Localize.shared.localizedText(withText: "language", withLocalizationFileNmae: "zh-Hant")
iconChineseImageView.image = Localize.shared.localizedImage(withImageName: "Chinese", withLocalizationFileNmae: "zh-Hant")
}
``
GitHub - LocalizationAppInsideChangeDemo